<template>
  <div id="main" class="pie-wrap"></div>
</template>

<script>
import * as echarts from "echarts";
export default {
  data() {
    return {};
  },
  mounted() {
    this.$nextTick(() => {
      this.drawChart();
    });

    window.addEventListener("resize", () => {
      this.myChart().resize();
    });
  },
  computed: {
    myChart() {
      return echarts.init(document.getElementById("main"));
    },
  },
  methods: {
    drawChart() {
      echarts.init(document.getElementById("main")).dispose();
      this.chart = echarts.init(document.getElementById("main"));
      this.chart.setOption({
        tooltip: {
          trigger: "axis",
          axisPointer: {
            // 坐标轴指示器，坐标轴触发有效
            type: "shadow", // 默认为直线，可选为：'line' | 'shadow'
          },
        },
        legend: {
          data: ["本生", "研究生"],
        },
        grid: {
          left: "3%",
          right: "4%",
          bottom: "3%",
          containLabel: true,
        },
        xAxis: [
          {
            type: "value",
          },
        ],
        yAxis: [
          {
            type: "category",
            axisTick: {
              show: false,
            },
            data: [
              "信息传输、软件和信息技术服务业",
              "公共管理、社会保障和社会组织",
              "金融业",
              "军队",
              "房地产业",
              "教育",
            ],
          },
        ],
        series: [
          {
            name: "本科生",
            type: "bar",
            stack: "总量",
            label: {
              show: true,
            },
            emphasis: {
              focus: "series",
            },
            data: [320, 302, 341, 374, 390, 450, 420],
          },
          {
            name: "研究生",
            type: "bar",
            stack: "总量",
            label: {
              show: true,
              position: "left",
            },
            emphasis: {
              focus: "series",
            },
            data: [-120, -132, -101, -134, -190, -230, -210],
          },
        ],
      });
    },
    resize() {
      this.myChart().resize();
    },
  },
};
</script>

<style scope>
.pie-wrap {
  width: 100%;
  height: 100%;
}
</style>
